package com.kongfuzi.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kongfuzi.student.R;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.bean.User;
import com.kongfuzi.student.support.utils.MD5Util;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.LogUtil;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.support.utils.Util;
import com.kongfuzi.student.support.volley.Response;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class LoginActivity extends CustomerActionBarActivity implements View.OnClickListener {


    @Bind(R.id.et_number)
    EditText mEt_number;
    @Bind(R.id.et_pwd)
    EditText mEt_pwd;
    @Bind(R.id.btn_login)
    Button mBtn_login;
    @Bind(R.id.tv_reg)
    TextView mTv_reg;
    @Bind(R.id.tv_forget)
    TextView mTv_forget;

    String num;
    String pwd;

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), LoginActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setCustomerBackVisibility(false);
        initEvents();
    }

    private void initEvents() {
        mBtn_login.setOnClickListener(this);
        mTv_reg.setOnClickListener(this);
        mTv_forget.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_reg:
                startActivity(RegisterActivity.newIntent());
                finish();
                rightEnter();
                break;
            case R.id.tv_forget:
                startActivity(RegisterActivity.startForgetInstance());
                rightEnter();
                break;
        }
    }

    private void login() {
        if (checkData()) {
            showProgressDialogNotCancel("正在登录");
            String requestUrl = UrlConstants.LOG_IN + "&mobile=" + num + "&pwd=" + MD5Util.encode
                    (pwd);
            RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dismissProgressDialog();
                    if(response!=null && response.optBoolean("success")) {
                        Gson gson = new Gson();
                        User user = gson.fromJson(response.optJSONObject("data").toString(), User
                                .class);
                        GasApplication.saveUser(user);
                        LogUtil.showInfoLog(mTag,user.toString());
                        EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.REFRESH_MENU_CONTENT));
                        ToastUtil.showToast(LoginActivity.this, "登录成功");
                        finish();
                    }
                }
            },null,mTag);
        }
    }

    private boolean checkData() {
        num = mEt_number.getText().toString().trim();
        pwd = mEt_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            ToastUtil.showToast(this, "请输入手机号！");
            return false;
        }
        if (!Util.isMoblie(num)) {
            ToastUtil.showToast(this, "请输入正确手机号！");
            return false;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.showToast(this, "请输入密码！");
            return false;
        }
        if (pwd.length() < 6) {
            ToastUtil.showToast(this, "密码最少6位！");
            return false;
        }
        return true;
    }
}
